package ltd.cheers.mall.controller.mall;

import ltd.cheers.mall.common.Constants;
import ltd.cheers.mall.common.ServiceResultEnum;
import ltd.cheers.mall.controller.vo.CheersMallShoppingCartItemVO;
import ltd.cheers.mall.controller.vo.CheersMallUserVO;
import ltd.cheers.mall.entity.CheersMallShoppingCartItem;
import ltd.cheers.mall.service.CheersMallShoppingCartService;
import ltd.cheers.mall.util.ResultGenerator;
import ltd.cheers.mall.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: LiHuanYang
 * @date: 2020/3/25 下午 04:12
 */


@Controller
public class ShoppingCartController {

    @Resource
    private CheersMallShoppingCartService cheersMallShoppingCartService;

    @GetMapping("/shop-cart")
    public String cartListPage(HttpServletRequest request,
                               HttpSession httpSession) {
        CheersMallUserVO user = (CheersMallUserVO) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY);
        int itemsTotal = 0;
        int priceTotal = 0;
        List<CheersMallShoppingCartItemVO> myShoppingCartItems = cheersMallShoppingCartService.getMyShoppingCartItems(user.getUserId());
        if (!CollectionUtils.isEmpty(myShoppingCartItems)) {
            //购物项总数
            itemsTotal = myShoppingCartItems.stream().mapToInt(CheersMallShoppingCartItemVO::getGoodsCount).sum();
            if (itemsTotal < 1) {
                return "error/error_5xx";
            }
            //总价
            for (CheersMallShoppingCartItemVO cheersMallShoppingCartItemVO : myShoppingCartItems) {
                priceTotal += cheersMallShoppingCartItemVO.getGoodsCount() * cheersMallShoppingCartItemVO.getSellingPrice();
            }
            if (priceTotal < 1) {
                return "error/error_5xx";
            }
        }
        request.setAttribute("itemsTotal", itemsTotal);
        request.setAttribute("priceTotal", priceTotal);
        request.setAttribute("myShoppingCartItems", myShoppingCartItems);
        return "mall/cart";
    }

    @PostMapping("/shop-cart")
    @ResponseBody
    public Result saveCheersMallShoppingCartItem(@RequestBody CheersMallShoppingCartItem cheersMallShoppingCartItem,
                                                 HttpSession httpSession) {
        CheersMallUserVO user = (CheersMallUserVO) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY);
        cheersMallShoppingCartItem.setUserId(user.getUserId());
        //todo 判断数量
        String saveResult = cheersMallShoppingCartService.saveCheersMallCartItem(cheersMallShoppingCartItem);
        //添加成功
        if (ServiceResultEnum.SUCCESS.getResult().equals(saveResult)) {
            return ResultGenerator.genSuccessResult();
        }
        //添加失败
        return ResultGenerator.genFailResult(saveResult);
    }

    @PutMapping("/shop-cart")
    @ResponseBody
    public Result updateCheersMallShoppingCartItem(@RequestBody CheersMallShoppingCartItem cheersMallShoppingCartItem,
                                                   HttpSession httpSession) {
        CheersMallUserVO user = (CheersMallUserVO) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY);
        cheersMallShoppingCartItem.setUserId(user.getUserId());
        //todo 判断数量
        String updateResult = cheersMallShoppingCartService.updateCheersMallCartItem(cheersMallShoppingCartItem);
        //修改成功
        if (ServiceResultEnum.SUCCESS.getResult().equals(updateResult)) {
            return ResultGenerator.genSuccessResult();
        }
        //修改失败
        return ResultGenerator.genFailResult(updateResult);
    }

    @DeleteMapping("/shop-cart/{cheersMallShoppingCartItemId}")
    @ResponseBody
    public Result updateCheersMallShoppingCartItem(@PathVariable("cheersMallShoppingCartItemId") Long cheersMallShoppingCartItemId,
                                                   HttpSession httpSession) {
        CheersMallUserVO user = (CheersMallUserVO) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY);
        Boolean deleteResult = cheersMallShoppingCartService.deleteById(cheersMallShoppingCartItemId);
        //删除成功
        if (deleteResult) {
            return ResultGenerator.genSuccessResult();
        }
        //删除失败
        return ResultGenerator.genFailResult(ServiceResultEnum.OPERATE_ERROR.getResult());
    }

    @GetMapping("/shop-cart/settle")
    public String settlePage(HttpServletRequest request,
                             HttpSession httpSession) {
        int priceTotal = 0;
        CheersMallUserVO user = (CheersMallUserVO) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY);
        List<CheersMallShoppingCartItemVO> myShoppingCartItems = cheersMallShoppingCartService.getMyShoppingCartItems(user.getUserId());
        if (CollectionUtils.isEmpty(myShoppingCartItems)) {
            //无数据则不跳转至结算页
            return "/shop-cart";
        } else {
            //总价
            for (CheersMallShoppingCartItemVO cheersMallShoppingCartItemVO : myShoppingCartItems) {
                priceTotal += cheersMallShoppingCartItemVO.getGoodsCount() * cheersMallShoppingCartItemVO.getSellingPrice();
            }
            if (priceTotal < 1) {
                return "error/error_5xx";
            }
        }
        request.setAttribute("priceTotal", priceTotal);
        request.setAttribute("myShoppingCartItems", myShoppingCartItems);
        return "mall/order-settle";
    }
}
